using Protocol.Code;
using Protocol.Dto;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UserHandler : HandlerBase
{
    public override void OnReceive(int subCode, object value)
    {
        switch (subCode)
        {
            case UserCode.CREATE_SRES:
                createRespose((int)value);
                break;
            case UserCode.GET_INFO_SRES:
                getInfoRespose(value as UserDto);
                break;
            case UserCode.ONLINE_SRES:
                onlineRespose((int)value);
                break;
            default:
                break;
        }
    }

    SocketMsg socketMsg = new SocketMsg();
    /// <summary>
    /// 获取信息的回应
    /// </summary>
    private void getInfoRespose(UserDto user)
    {
        if(user == null)
        {
            // 没有角色
            // 显示创建面板
            Dispatch(AreaCode.UI, UIEvent.CREATE_PANEL_ACTIVE, true);
        }
        else
        {
            // 有角色
            // 隐藏创建面板
            Dispatch(AreaCode.UI, UIEvent.CREATE_PANEL_ACTIVE, false);

            // 角色上线
            //socketMsg.Change(OpCode.USER, UserCode.ONLINE_CREQ, null);
            //Dispatch(AreaCode.NET, 0, socketMsg);

            GameModel model = Models.GameModel;
            model.UserDto = user;

            // 更新一下本地的显示面板
            Dispatch(AreaCode.UI, UIEvent.REFLESH_INFO_PANEL, user);
        }
    }

    /// <summary>
    /// 上线的响应
    /// </summary>
    /// <param name="result"></param>
    private void onlineRespose(int result)
    {
        if(result == 0)
        {
            // 上线成功
            Debug.Log("上线成功"); 
        }
        else if(result == -1)
        {
            Debug.LogError("客户端非法登录");
        }
        else if(result == -2)
        {
            Debug.LogError("非法操作,没有角色就上线了");
        }

    }

    /// <summary>
    /// 创建的相应
    /// </summary>
    /// <param name="result"></param>
    private void createRespose(int result)
    {
        if (result == -1)
        {
            Debug.LogError("客户端非法登录");
        }
        else if (result == -2)
        {
            Debug.LogError("已经有角色,重复创建");
        }
        else if(result == 0)
        {
            // 创建成功
            // 隐藏创建面板
            Dispatch(AreaCode.UI, UIEvent.CREATE_PANEL_ACTIVE, false);
            // 获取角色信息
            socketMsg.Change(OpCode.USER, UserCode.GET_INFO_CREQ, null);
            Dispatch(AreaCode.NET, 0, socketMsg);

        }
    }

}
